﻿Public Class RgbHistogram


    Public Shared red(255) As Integer
    Public Shared green(255) As Integer
    Public Shared blue(255) As Integer

    Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        Dim image As Bitmap
        Dim imageGraphics As Graphics
        Dim x As Integer

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim redPen As New Pen(Color.FromArgb(255, 255, 0, 0), 3)
        For Each i As Integer In red
            If Not i = 0 Then
                imageGraphics.DrawLine(redPen, x, 200, x, 200 - CInt(i * NumericUpDown1.Value))
            End If
            x += 4
        Next
        PictureBox1.Image = image

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 3)
        For Each i As Integer In green
            If Not i = 0 Then
                imageGraphics.DrawLine(greenPen, x, 200, x, 200 - CInt(i * NumericUpDown1.Value))
            End If
            x += 4
        Next
        PictureBox2.Image = image

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim bluePen As New Pen(Color.FromArgb(255, 0, 0, 255), 3)
        For Each i As Integer In blue
            If Not i = 0 Then
                imageGraphics.DrawLine(bluePen, x, 200, x, 200 - CInt(i * NumericUpDown1.Value))
            End If
            x += 4
        Next
        PictureBox3.Image = image
        If Not NumericUpDown1.Value < 0.0001 Then
            lblRed.Text = CInt(CDec(200) / NumericUpDown1.Value)
            lblGreen.Text = CInt(CDec(200) / NumericUpDown1.Value)
            lblBlue.Text = CInt(CDec(200) / NumericUpDown1.Value)

        End If

    End Sub




    Private Sub AcToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AcToolStripMenuItem.Click
        Dim a As Bitmap
        Dim result As DialogResult = OpenFileDialog1.ShowDialog()

        If result = DialogResult.OK Then
            Dim file As String = OpenFileDialog1.FileName

            Try
                a = New Bitmap(file)
            Catch
                MsgBox("Bozuk Dosya...")
                Exit Sub
            End Try
        Else
            Exit Sub
        End If

        For i = 0 To a.Width - 1
            For j = 0 To a.Height - 1
                red(a.GetPixel(i, j).R) += 1
                green(a.GetPixel(i, j).G) += 1
                blue(a.GetPixel(i, j).B) += 1
            Next
        Next

        Dim max As Integer

        If red.Max() > green.Max Then
            If red.Max() > blue.Max Then
                max = red.Max
            Else
                max = blue.Max
            End If
        Else
            If green.Max() > blue.Max Then
                max = green.Max
            Else
                max = blue.Max
            End If
        End If

        lblMaxRed.Text = red.Max
        lblMaxGreen.Text = green.Max
        lblMaxBlue.Text = blue.Max

        Dim katSayi As Double

        If max > 0 And max <= 200 Then
            lblRed.Text = "200"
            lblGreen.Text = "200"
            lblBlue.Text = "200"
            katSayi = 1
        ElseIf max > 200 And max <= 10000 Then
            lblRed.Text = "10.000"
            lblGreen.Text = "10.000"
            lblBlue.Text = "10.000"
            katSayi = 0.02
        ElseIf max > 10000 And max <= 20000 Then
            lblRed.Text = "20.000"
            lblGreen.Text = "20.000"
            lblBlue.Text = "20.000"
            katSayi = 0.01
        ElseIf max > 20000 And max <= 30000 Then
            lblRed.Text = "30.000"
            lblGreen.Text = "30.000"
            lblBlue.Text = "30.000"
            katSayi = 0.0066666666666666671
        ElseIf max > 30000 And max <= 40000 Then
            lblRed.Text = "40.000"
            lblGreen.Text = "40.000"
            lblBlue.Text = "40.000"
            katSayi = 0.0050000000000000001
        ElseIf max > 40000 And max <= 50000 Then
            lblRed.Text = "50.000"
            lblGreen.Text = "50.000"
            lblBlue.Text = "50.000"
            katSayi = 0.0040000000000000001
        ElseIf max > 50000 And max <= 60000 Then
            lblRed.Text = "60.000"
            lblGreen.Text = "60.000"
            lblBlue.Text = "60.000"
            katSayi = 0.0033333333333333335
        ElseIf max > 60000 And max <= 70000 Then
            lblRed.Text = "70.000"
            lblGreen.Text = "70.000"
            lblBlue.Text = "70.000"
            katSayi = 0.0028571428571428571
        ElseIf max > 70000 And max <= 80000 Then
            lblRed.Text = "80.000"
            lblGreen.Text = "80.000"
            lblBlue.Text = "80.000"
            katSayi = 0.0025000000000000001
        ElseIf max > 80000 And max <= 90000 Then
            lblRed.Text = "90.000"
            lblGreen.Text = "90.000"
            lblBlue.Text = "90.000"
            katSayi = 0.0022222222222222222
        ElseIf max > 90000 And max <= 100000 Then
            lblRed.Text = "100.000"
            lblGreen.Text = "100.000"
            lblBlue.Text = "100.000"
            katSayi = 0.002
        ElseIf max > 100000 And max <= 1000000 Then
            lblRed.Text = "1.000.000"
            lblGreen.Text = "1.000.000"
            lblBlue.Text = "1.000.000"
            katSayi = 0.00020000000000000001
        ElseIf max > 1000000 And max <= 2000000 Then
            lblRed.Text = "2.000.000"
            lblGreen.Text = "2.000.000"
            lblBlue.Text = "2.000.000"
            katSayi = 0.0001
        ElseIf max > 2000000 Then
            lblRed.Text = "+++"
            lblGreen.Text = "+++"
            lblBlue.Text = "+++"
            katSayi = 0.0001

        End If

        Dim image As Bitmap
        Dim imageGraphics As Graphics
        Dim x As Integer

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim redPen As New Pen(Color.FromArgb(255, 255, 0, 0), 3)
        For Each i As Integer In red
            If Not i = 0 Then
                imageGraphics.DrawLine(redPen, x, 200, x, 200 - CInt(i * katSayi))
            End If
            x += 4
        Next
        PictureBox1.Image = image

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 3)
        For Each i As Integer In green
            If Not i = 0 Then
                imageGraphics.DrawLine(greenPen, x, 200, x, 200 - CInt(i * katSayi))
            End If
            x += 4
        Next
        PictureBox2.Image = image

        image = New Bitmap(1024, 200)
        imageGraphics = Graphics.FromImage(image)

        x = 0
        Dim bluePen As New Pen(Color.FromArgb(255, 0, 0, 255), 3)
        For Each i As Integer In blue
            If Not i = 0 Then
                imageGraphics.DrawLine(bluePen, x, 200, x, 200 - CInt(i * katSayi))
            End If
            x += 4
        Next
        PictureBox3.Image = image

        NumericUpDown1.Value = katSayi

    End Sub
End Class
